package com.iwinding.ladygg.net;

import android.content.Context;
import android.util.Log;

import com.iwinding.ladygg.constant.Constant;
import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.request.ResourcesReq;
import com.iwinding.ladygg.utils.DateUtil;
import com.iwinding.ladygg.utils.GsonUtil;
import com.iwinding.ladygg.utils.SPUtils;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import internal.org.apache.http.entity.mime.HttpMultipartMode;
import internal.org.apache.http.entity.mime.MultipartEntity;
import internal.org.apache.http.entity.mime.content.FileBody;

/**
 * Created by 82218 on 2017/6/9.
 */

public class UploadNet {
    private static String zhuanyi(String url){
        if (url.contains(" ")){
            if(url.substring(url.length()-1)==" "){
                url= url.substring(0,url.length()-1);
            }else{
                url= url.replace(" ","%20");
            }
        }
        if (url.contains("\"")){
            url= url.replace("\"","%22");
        }
        if (url.contains("{")){
            url= url.replace("{","%7B");
        }
        if (url.contains("}")){
            url= url.replace("{","%7D");
        }
        return url;
    }

    public static void upload(Context mContext, File file) {
        String parmas = "?json=" + GsonUtil.getInstance().toJson(getResourcesReq());
        String strUrl = Constant.NetUrl.PROHTTP + Constant.NetUrl.RESOURCES +parmas + "&access_token=" + SPUtils.getToken(mContext);

//        URL url = null;
//        URI uri = null;
//        try {
//            url = new URL(strUrl);
//            uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
//
//        if(uri==null)
//            return;
        String encodedurl = null;
        try {
            encodedurl = URLEncoder.encode(strUrl,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(encodedurl==null)
            return;
        HttpPost httpPost = new HttpPost(encodedurl);
        httpPost.addHeader(new BasicHeader("Cookie", "JSESSIONID=" + SPUtils.getSpPropertyString(mContext, SPConstant.COOKIE, "")));
        FileBody fileBody = new FileBody(file);
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, DateUtil.getCurrentTimeInString(),
                Charset.forName("UTF-8"));
        multipartEntity.addPart("headImg", fileBody);
        httpPost.setEntity(multipartEntity);
        BasicHttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 60 * 1000);
        HttpConnectionParams.setSoTimeout(params, 60 * 1000);
        HttpClient httpClient = new DefaultHttpClient();
        httpPost.setParams(params);
        try {
            HttpResponse httpResponse = httpClient.execute(httpPost);
            BufferedReader in = new BufferedReader(new InputStreamReader(httpResponse.getEntity()
                    .getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String content = sb.toString();
            Log.i("@hzy", "-0-" + URLDecoder.decode(content));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

//    private static Map<String, String> getHeaders() {
//        HashMap<String, String> headers = new HashMap<String, String>();
//
//        headers.put("region_code", "CN");//大陆版
//        headers.put("language_code", DeviceUtil.getLangCode());
//        headers.put("screen_size", DeviceUtil.getScreenSize(mContext));
//
//        //ucmp头
//        headers.put("mediaCode", "Android");
//        headers.put("systemCode", "syt");
//
//        headers.put("platform", "Android");
//        headers.put("client_ver", AppUtil.getVersionName(mContext));
//
//        headers.put("protocol_ver", String.valueOf(AppUtil.getVersionCode(mContext)));
//        headers.put("model", DeviceUtil.getModel());
//        headers.put("carrier", ""); // not necessary
//        headers.put("deviceId", DeviceUtil.getDeviceId(mContext));
//        headers.put("sessionId", PreferenceHelper.getSessionid(mContext));
//        headers.put("Content-Type", "application/x-www-form-urlencoded");
//
//        return headers;
//    }

    private static ResourcesReq getResourcesReq() {
        ResourcesReq resourcesReq = new ResourcesReq();
        resourcesReq.uri = Constant.NetUrl.UPLOADHEADIMG;
        resourcesReq.reqTime = DateUtil.getCurrentTimeInString();
        resourcesReq.params = "";
        return resourcesReq;
    }

}
